import plotly.express as px
from pywebio.output import *
from  common.config import yawConfig as Config
import plotly.graph_objects as go
import pandas as pd
from plotly.subplots import make_subplots
from back.web.styleConfig import  tableStyle

def yaw_hisInitialData():
    X = pd.read_csv(Config.hisInitialData)
    X = X if len(X) < Config.displayMaxPoints else X.sample(Config.displayMaxPoints)
    fig = px.scatter_3d(X,
                        x=Config.WindSpeed, y=Config.WindDirection, z=Config.ActPower,
                        size=Config.WindSpeed,
                        size_max=Config.size_max,
                        opacity=Config.opacity,
                        color=Config.ActPower)
    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0), height=Config.scatter_3d_height)

    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))
    # with use_scope("table_scope", clear=True):
    #     put_table(pd.read_csv(Config.hisInitialDataAttr, header=None).values.tolist())
    #     put_html(tableStyle)
def yaw_hisCleanProcess():
    with use_scope("table_scope", clear=True):
        put_table(pd.read_csv(Config.hisCleanAttr, header=None).values.tolist())
        put_html(tableStyle)

    X = pd.read_csv(Config.hisCleanProcess)
    X = X if len(X) < Config.displayMaxPoints else X.sample(Config.displayMaxPoints)
    fig = px.scatter_3d(X,
                        x=Config.WindSpeed, y=Config.WindDirection, z=Config.ActPower,
                        size=Config.WindSpeed,
                        size_max=Config.size_max,
                        opacity=Config.opacity,
                        color=Config.ActPower)
    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0), height=Config.scatter_3d_height)
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))
def yaw_hisGroupProcess():
    with use_scope("table_scope", clear=True):
        put_table(pd.read_csv(Config.hisGroupAttr, header=None).values.tolist())
        put_html(tableStyle)

    fig = px.scatter_3d(pd.read_csv(Config.hisGroupProcess),
                        x=Config.WindSpeed, y=Config.WindDirection, z=Config.ActPower,
                        size=Config.WindSpeed,
                        size_max=Config.size_max,
                        opacity=Config.opacity,
                        color=Config.ActPower)
    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0), height=Config.scatter_3d_height)
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))
def yaw_hisSmoothProcess():
    with use_scope("table_scope", clear=True):
        put_table(pd.read_csv(Config.hisSmoothAttr, header=None).values.tolist())
        put_html(tableStyle)

    fig = px.scatter_3d(pd.read_csv(Config.hisSmoothProcess),
                        x=Config.WindSpeed, y=Config.WindDirection, z=Config.ActPower,
                        size=Config.WindSpeed,
                        size_max=Config.size_max,
                        opacity=Config.opacity,
                        color=Config.ActPower)
    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0), height=Config.scatter_3d_height)
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))
def yaw_hisSurfaceProcess():
    with use_scope("table_scope", clear=True):
        put_table(pd.read_csv(Config.hisSurfaceAttr, header=None).values.tolist())
        put_html(tableStyle)

    fig = px.scatter_3d(pd.read_csv(Config.hisSurfaceProcess),
                        x=Config.WindSpeed, y=Config.WindDirection, z=Config.ActPower,
                        size=Config.WindSpeed,
                        size_max=Config.size_max,
                        opacity=Config.opacity,
                        color=Config.ActPower)
    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0), height=Config.scatter_3d_height)
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))
def yaw_hisSurfaceAxes():
    with use_scope("table_scope", clear=True):
        put_table(pd.read_csv(Config.hisSurfaceAttr, header=None).values.tolist())
        put_html(tableStyle)

    X = pd.read_csv(Config.hisSurfaceProcess)
    x, y, z = X[Config.WindSpeed].values.tolist(), X[Config.WindDirection].values.tolist(), X[
        Config.ActPower].values.tolist()
    fig = go.Figure(data=[go.Mesh3d(x=x,
                                    y=y,
                                    z=z,
                                    opacity=0.7,
                                    color='rgba(100,22,200,0.9)'
                                    )])
    fig.update_layout(scene=dict(
        xaxis_title=Config.WindSpeed,
        yaxis_title=Config.WindDirection,
        zaxis_title=Config.ActPower),
        # width=700,
        margin=dict(l=0, r=0, b=0, t=0), height=Config.scatter_3d_height)
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))
def yaw_hisSubSurfaceAxes():
    with use_scope("table_scope", clear=True):
        put_table(pd.read_csv(Config.hisSurfaceAttr, header=None).values.tolist())
        put_html(tableStyle)

    X = pd.read_csv(Config.hisSurfaceProcess)
    x, y, z = X[Config.WindSpeed].values.tolist(), X[Config.WindDirection].values.tolist(), X[
        Config.ActPower].values.tolist()
    Y = pd.read_csv(Config.hisSurfaceProcess)
    x1, y1, z1 = Y[Config.WindSpeed].values.tolist(), Y[Config.WindDirection].values.tolist(), Y[
        Config.ActPower].values.tolist()
    fig = make_subplots(
        rows=1, cols=2,
        specs=[[{'type': 'surface'}, {'type': 'surface'}]])
    fig.add_trace(
        go.Mesh3d(x=x,
                  y=y,
                  z=z,
                  opacity=0.7,
                  color='rgba(100,22,200,0.9)',
                  name='历史数据'
                  ),
        row=1, col=1)
    fig.add_trace(
        go.Mesh3d(x=x1,
                  y=y1,
                  z=z1,
                  opacity=0.7,
                  color='rgba(10,2,1,0.9)',
                  name='更新数据'
                  ),
        row=1, col=2)

    fig.update_layout(scene=dict(
        xaxis_title=Config.WindSpeed,
        yaxis_title=Config.WindDirection,
        zaxis_title=Config.ActPower),
        width=700,
        margin=dict(r=0, b=0, l=0, t=0))
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))
def yaw_hisLineRegress():
    with use_scope("table_scope", clear=True):
        put_table(pd.read_csv(Config.hisLineRegressAttr, header=None).values.tolist())
        put_html(tableStyle)

    X = pd.read_csv(Config.hisLineRegress)
    fig = px.line(X, x=Config.WindDirection, y=Config.WindSpeed, title='偏航误差曲线',
                  range_x=[-Config.misalignment, Config.misalignment])
    fig.update_layout(title='偏航误差识别', xaxis_title='风向偏差',yaxis_title='风速[m/s]')
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))
